package com.example.exception;

import com.example.util.Response;
import com.example.util.ResponseResult;
import com.example.util.StatusCode;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Author:20201002983_郑雨蓓
 * Date: 2022/12/25
 * Description:
 */
@RestControllerAdvice
public class GlobalExceptionHandle {
    //处理异常的方法1.  并确定接收的是哪种类型的异常
    @ExceptionHandler(Exception.class)
    public ResponseResult exceptionHandler(Exception e) {
        // 捕获到某个指定的异常，比如是 NotAllowedRegException 类型
        if (e instanceof NotAllowException) {
            //处理结束后 还是要返回统一相应结果类
            return Response.createFailResp(StatusCode.NOT_ALLOWRD_REG.code, "异常: 此书籍尚未创建");
        } else {
            //处理其它类型的异常 可以进入到不同的分支
            return Response.createFailResp();
        }
    }

    //捕获AuthorizationException的异常
    @ExceptionHandler(value = AuthorizationException.class)
    @ResponseBody
    public ResponseResult<Object> handleException(AuthorizationException e) {
        return Response.createFailResp("捕获AuthorizationException的异常,权限不足！！！！！");
    }
    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public ResponseResult<Object> exceptionHandler(BindException e) {
        return Response.createFailResp("提交的参数信息有误");
    }
}
